# ---------- ---------- ---------- ---------- ---------- ----------
#
# med-enterprise-dash Makefile
#
#    1) config, vars, and target
#    2) functions
#    3) phonies
#
# ---------- ---------- ---------- ---------- ---------- ----------

target: help


# ---------- ---------- ---------- ---------- ---------- ----------
# functions
# ---------- ---------- ---------- ---------- ---------- ----------

define fix
	@black --exclude=var/ ./ ./bin/* || \
            echo "black returned nonzero $$?"
endef

define lint
	@flake8 --exclude=var/ --max-line-length 88 ./src/med ./tests || \
            echo "flake8 returned nonzero $$?"
	@flake8 --max-line-length 88 --ignore E402 ./bin/* || \
            echo "flake8 returned nonzero $$?"
endef


# ---------- ---------- ---------- ---------- ---------- ----------
# phonies
# ---------- ---------- ---------- ---------- ---------- ----------


.PHONY: help
help:
	@echo "Usage: make [PHONY]"
	@sed -n -e '/sed/! s/\.PHONY: //p' Makefile


.PHONY: clean
clean:
	@py3clean . || echo "pyclean not installed"
	@rm -rf ./var


.PHONY: dev
dev:
	@$(call fix)
	@$(call lint)


.PHONY: requirements
requirements:
	@poetry export \
            -f requirements.txt \
            -o requirements.txt

	@poetry export \
            --dev \
            -f requirements.txt \
            -o requirements.dev.txt


.PHONY: run
run:
	@./bin/run_test_server


.PHONY: rerun
rerun: dev test wipe run


.PHONY: test
test:
	@python3 -m unittest discover -s tests -t . || \
            echo "unit tests failed"


.PHONY: venv
venv: requirements
	@python3 -m venv ./var/venv
	@./var/venv/bin/pip install --upgrade pip
	@./var/venv/bin/pip install -r requirements.dev.txt
	@echo "Now activate the venv:"
	@echo "    source ./var/venv/bin/activate"


.PHONY: wipe
wipe:
	@clear
